﻿using UnityEngine;
using UnityEngine.UIElements;

namespace winS.UnityEditor.UIElement
{
    public class CurveField : EditorField<AnimationCurve>
    {
        public Rect range { get => curveField.ranges; set => curveField.ranges = value; }

        private readonly global::UnityEditor.UIElements.CurveField curveField;

        public CurveField(ElementFactory elementFactory) : base(elementFactory)
        {
            Add(curveField = new global::UnityEditor.UIElements.CurveField());
            curveField.renderMode = global::UnityEditor.UIElements.CurveField.RenderMode.Mesh;
            EditorElementUtility.ApplySubFieldStyle(curveField);
            curveField.RegisterValueChangedCallback(OnValueChangeCallback);
        }

        public override void SetValueWithoutNotify(AnimationCurve value)
        {
            base.SetValueWithoutNotify(value);
            curveField.SetValueWithoutNotify(value);
        }

        protected override void SetValue(AnimationCurve value)
        {
            base.SetValue(value);
            curveField.SetValueWithoutNotify(value);
        }

        private void OnValueChangeCallback(ChangeEvent<AnimationCurve> changeEvent)
        {
            base.SetValue(changeEvent.newValue);
        }
    }
}